package springtest.webRsa;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Base64;
import java.util.HashMap;

import org.junit.Test;

import springtest.pojo.Device_Change;

/**
 * @author chenzhiqiang
 * @date 2018-05-08 10:07:23
 */
public class TestRsa {
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        HashMap<String, Object> map = RSAUtils.getKeys();
        // 生成公钥和私钥
        RSAPublicKey publicKey = (RSAPublicKey) map.get("public");
        System.out.println("公钥：" + new String(Base64.getEncoder().encode(publicKey.getEncoded())));
        RSAPrivateKey privateKey = (RSAPrivateKey) map.get("private");
        System.out.println("私钥：" + new String(Base64.getEncoder().encode(privateKey.getEncoded())));
        // 模
        String modulus = publicKey.getModulus().toString();
        String publicmodulus = publicKey.getModulus().toString();
        String privatemodulus = privateKey.getModulus().toString();
        System.out.println("共有key模:" + publicmodulus);
        System.out.println("私有key模:" + privatemodulus);
        // 公钥指数
        String public_exponent = publicKey.getPublicExponent().toString();
        // 私钥指数
        String private_exponent = privateKey.getPrivateExponent().toString();
        // 明文
        String ming = "66666";
        // 使用模和指数生成公钥和私钥
        RSAPublicKey pubKey = RSAUtils.getPublicKey(modulus, public_exponent);
        RSAPrivateKey priKey = RSAUtils.getPrivateKey(modulus, private_exponent);
        // 加密后的密文
        String mi = RSAUtils.encryptByPublicKey(ming, publicKey);
        // System.err.println(mi);
        System.out.println(mi);
        System.out.println("------------------");
        // 解密后的明文
        ming = RSAUtils.decryptByPrivateKey(mi, privateKey);
        System.out.println(ming);
        // System.err.println(ming);
    }

    public static String getDeviceChange() {
        Device_Change change = new Device_Change();
        change.set_id("111");
        change.setUuid("222");
        change.setName("测试");
        return change.toString();
    }

    @Test
    public void geKeys() throws NoSuchAlgorithmException {
        HashMap<String, Object> map = new HashMap<String, Object>();
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
        // keyPairGen.initialize(1024);
        keyPairGen.initialize(64);
        KeyPair keyPair = keyPairGen.generateKeyPair();
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        map.put("public", publicKey);
        map.put("private", privateKey);
        System.out.println(publicKey);
        System.out.println("----------------------");
        System.out.println(privateKey);
    }

    @Test
    public void a() throws CertificateException {
        CertificateFactory.getInstance("");
    }
}
